const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const Inventory = sequelize.define('Inventory', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    product_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      unique: true,
      comment: '产品ID'
    },
    current_stock: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '当前库存'
    },
    available_stock: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: '可用库存'
    },
    reserved_stock: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '预留库存'
    },
    in_transit_stock: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '在途库存'
    },
    min_stock: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '最小库存'
    },
    max_stock: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '最大库存'
    },
    reorder_point: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '再订货点'
    },
    reorder_quantity: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      comment: '再订货数量'
    },
    average_cost: {
      type: DataTypes.DECIMAL(10, 2),
      defaultValue: 0,
      comment: '平均成本'
    },
    total_value: {
      type: DataTypes.DECIMAL(12, 2),
      defaultValue: 0,
      comment: '库存总价值'
    },
    warehouse_location: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '仓库位置'
    },
    last_count_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '最后盘点日期'
    },
    status: {
      type: DataTypes.ENUM('normal', 'low_stock', 'out_of_stock', 'overstock'),
      defaultValue: 'normal',
      comment: '库存状态'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'inventory',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '库存表',
    indexes: [
      {
        unique: true,
        fields: ['product_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['current_stock']
      }
    ]
  })

  return Inventory
}